import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test_demo/three/animate/scale_widget.dart';
import 'package:flutter_test_demo/three/animate/up_down_widget.dart';

import 'animate/show_hide_widget.dart';

class AnimatePage extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    return _AnimateAppState();
  }
}

class _AnimateAppState extends State<AnimatePage> with SingleTickerProviderStateMixin {

  late AnimationController controller;
  late Animation<double> animation;

  @override
  void initState() {
    super.initState();
    // 创建 AnimationController 对象
    controller = AnimationController(
        vsync: this, duration: const Duration(milliseconds: 2000));
    // 通过 Tween 对象 创建 Animation 对象
    animation = Tween(begin: 50.0, end: 200.0).animate(controller)
      ..addListener(() {
        setState(() {});
      })
      ..addStatusListener((status) {
        if (status == AnimationStatus.completed) {
          controller.reverse();
        } else if (status == AnimationStatus.dismissed) {
          controller.forward();
        }
      });
    // 执行动画
    controller.forward();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
            appBar: AppBar(
              title: Text('AnimateApp'),
            ),
            body: Column(
              children: [
                ScaleWidget(
                  Container(
                    // 获取动画的值赋给 widget 的宽高
                    width: 100,//animation.value,
                    height: 100,//animation.value,
                    decoration: BoxDecoration(
                        color: Colors.redAccent
                    ),

                  ),
                ),

                const Padding(padding: EdgeInsets.all(10)),

                UpDownWidget(
                  Container(
                    // 获取动画的值赋给 widget 的宽高
                    width: 100,//animation.value,
                    height: 100,//animation.value,
                    decoration: BoxDecoration(
                        color: Colors.redAccent
                    ),

                  ),
                ),

                const Padding(padding: EdgeInsets.all(10)),

                ShowHideWidget(
                  Container(
                    // 获取动画的值赋给 widget 的宽高
                    width: 100,//animation.value,
                    height: 100,//animation.value,
                    decoration: BoxDecoration(
                        color: Colors.redAccent
                    ),
                  ),
                  showToHide: true,
                ),

                const Padding(padding: EdgeInsets.all(10)),

                ShowHideWidget(
                  Container(
                    // 获取动画的值赋给 widget 的宽高
                    width: 100,//animation.value,
                    height: 100,//animation.value,
                    decoration: BoxDecoration(
                        color: Colors.redAccent
                    ),

                  ),
                ),

              ],
            ),
    );
  }

  @override
  void dispose() {
    // 资源释放
    controller.dispose();
    super.dispose();
  }
}